﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GoPlay.Models;

namespace GoPlay.Controllers
{
    [Authorize]
    public class ContactsController : Controller
    {
        IRepository<Contact> _repository;

        public ContactsController() : this(new EFRepository<Contact>()) { }

        public ContactsController(IRepository<Contact> repository)
        {
            _repository = repository;
        }


        //
        // GET: /Contacts/

        public ActionResult Index()
        {
            var results = _repository.GetAllByUserId(Convert.ToInt32(Session["UserId"]));
            ViewBag.Contacts = results;
            return View(results);
        }


        //public ActionResult Index(int userId)
        //{
        //    var results = _repository.GetAllByUserId(userId);
        //    return View(results);
        //}

        //
        // GET: /Contacts/Details/5

        public ActionResult Details(int id = 0)
        {
            Contact contact = _repository.GetByID(id);
            return View(contact);
        }

        //
        // GET: /Contacts/Create

        public ActionResult Create()
        {
            var Contact = new Contact();
            return View(Contact);
        }

        //
        // POST: /Contacts/Create

        [HttpPost]
        public ActionResult Create(Contact contact)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _repository.Insert(contact);
                    _repository.SaveChanges();
                    Session["FlashSuccess"] = "Your Contact was created.";
                    return RedirectToAction("Index");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
                Session["FlashError"] = ex.Message;
            }

            foreach (ModelState modelState in ViewData.ModelState.Values)
            {
                foreach (ModelError error in modelState.Errors)
                {
                    Session["FlashError"] += error.ErrorMessage;
                }
            }
            return View(contact);
        }

        //
        // GET: /Contacts/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Contact c = _repository.GetByID(id);
            if (c == null)
            {
                return HttpNotFound();
            }
            return View(c);
        }

        //
        // POST: /Contacts/Edit/5

        [HttpPost]
        public ActionResult Edit(Contact contact)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _repository.Update(contact);
                    _repository.SaveChanges();
                    Session["FlashSuccess"] = "Your Contact was updated.";
                    return RedirectToAction("Index");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
                Session["FlashError"] = ex.Message;
            }

            return View(contact);
        }

        //
        // GET: /Contacts/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Contact c = _repository.GetByID(id);
            if (c == null)
            {
                return HttpNotFound();
            }
            return View(c);
        }

        //
        // POST: /Contacts/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                Contact c = _repository.GetByID(id);
                _repository.Delete(c);
                _repository.SaveChanges();
                Session["FlashSuccess"] = "Your Contact was deleted.";
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
                Session["FlashError"] = ex.Message;
            }

            foreach (ModelState modelState in ViewData.ModelState.Values)
            {
                foreach (ModelError error in modelState.Errors)
                {
                    Session["FlashError"] += error.ErrorMessage;
                }
            }
            return View();

        }
    }
}